package site.edody.dframe.commet;

import com.alibaba.nacos.api.annotation.NacosInjected;
import com.alibaba.nacos.api.config.ConfigService;
import com.alibaba.nacos.api.exception.NacosException;
import org.springframework.stereotype.Component;
import org.ssssssss.magicapi.core.annotation.MagicModule;
import org.ssssssss.script.annotation.Comment;
import org.yaml.snakeyaml.Yaml;

import java.util.Properties;

/**
 * 获取配置信息
 */
//注入到Spring容器中
@Component
@MagicModule("nacosconfig")    // 模块名称
public class NacosConfig {

    @NacosInjected
    private ConfigService configService;
    /**
     *   获取配置
     * @param dataId 配置中心的dataId
     * @param group 配置中心的group
     */
    @Comment("获取nacos配置属性(仅适用于nacos)")
    public Properties getconfig(@Comment("dataId(nacos配置中心的dataId)")String dataId,@Comment("group(nacos配置中心的group)")String group) {
        Properties properties = null;
        try {
            String content = configService.getConfig(dataId, group, 5000);
            Yaml yaml = new Yaml();
            properties = yaml.loadAs(content,Properties.class);
        } catch (NacosException e) {
            e.printStackTrace();
        }
        return properties;
    }
}
